package at.htlkaindorf.jsf.session;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

import at.htlkaindorf.jsf.domain.UploadedImage;

@ManagedBean(name = "profilePictureBean")
@SessionScoped
public class ProfilePictureBean implements Serializable {

    private static final long serialVersionUID = -621585057936703875L;

    private UploadedImage imageFile;

    public void paint(OutputStream stream, Object object) throws IOException {
        stream.write(imageFile.getData());
        stream.close();
    }

    public void listener(FileUploadEvent event) throws Exception {
        UploadedFile item = event.getUploadedFile();

        UploadedImage file = new UploadedImage();
        byte[] data = item.getData();
        file.setLength(data.length);
        file.setName(item.getName());
        file.setData(data);

        this.imageFile = file;
    }

    public String clearUploadData() {
        imageFile = null;
        return null;
    }

    public long getTimeStamp() {
        return System.currentTimeMillis();
    }


    public UploadedImage getImageFile() {
        return imageFile;
    }

    public void setImageFile(UploadedImage imageFile) {
        this.imageFile = imageFile;
    }
}
